package com.leo.foodmaster.admin.stock.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.stock.entity.WarehouseInRecord;
import com.leo.foodmaster.admin.stock.entity.WarehouseInRecordDetails;
import com.leo.foodmaster.admin.stock.entity.WarehouseOutRecord;
import com.leo.foodmaster.admin.stock.entity.WarehouseOutRecordDetails;
import com.leo.foodmaster.admin.stock.service.IWarehouseOutRecordDetailsService;
import com.leo.foodmaster.admin.stock.service.IWarehouseOutRecordService;
import com.leo.foodmaster.admin.system.entity.Dept;
import com.leo.foodmaster.admin.system.service.IDeptService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 出库记录 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-09-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/stock/warehouseOutRecord")
public class WarehouseOutRecordController {
    private final IWarehouseOutRecordService warehouseOutRecordService;
    private final IWarehouseOutRecordDetailsService warehouseOutRecordDetailsService;
    private final IDeptService deptService;


    @ApiOperation("条件分页查询出库记录列表")
    @GetMapping("/list")
    public Result<IPage<WarehouseOutRecord>> getWarehouseOutRecordPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "仓库Id") @RequestParam(required = false) Long warehouseId,
            @ApiParam(value = "单据编号") @RequestParam(required = false) String serialNum,
            @ApiParam(value = "单据状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime
    ) {
        IPage<WarehouseOutRecord> pageList  = warehouseOutRecordService
                .getWarehouseOutRecordPageList(new Page<>(pageNum, pageSize), warehouseId,
                        serialNum, status, beginTime, endTime);
        return Result.success(pageList);
    }

    @ApiOperation("根据id查询出库记录详情")
    @GetMapping("/{id}")
    public Result<WarehouseOutRecord> getById(@ApiParam("入库记录Id") @PathVariable Long id){
        WarehouseOutRecord warehouseOutRecord = warehouseOutRecordService.getById(id);
        List<WarehouseOutRecordDetails> list = warehouseOutRecordDetailsService.list(Wrappers.lambdaQuery(WarehouseOutRecordDetails.class)
                .eq(WarehouseOutRecordDetails::getWarehouseOutRecordId, id));
        warehouseOutRecord.setDetailList(list);
        return Result.success(warehouseOutRecord);
    }

    @PostMapping
    @ApiOperation("新增出库记录")
    public Result<Object> addWarehouseOutRecord(@RequestBody WarehouseOutRecord warehouseOutRecord) {
        return warehouseOutRecordService.addWarehouseOutRecord(warehouseOutRecord);
    }

    @PutMapping
    @ApiOperation("修改出库记录")
    public Result<Object> updateWarehouseOutRecord(@RequestBody WarehouseOutRecord warehouseOutRecord) {
        return warehouseOutRecordService.updateWarehouseOutRecord(warehouseOutRecord);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除出库清单")
    public Result<Object> deleteWarehouseOutRecord(@ApiParam("出库清单Id") @PathVariable Long id) {
        return warehouseOutRecordService.deleteWarehouseOutRecord(id);
    }

    @PutMapping("/check")
    @ApiOperation("审核出库记录")
    public Result<Object> checkWarehouseOutRecord(@RequestBody WarehouseOutRecord warehouseOutRecord) {
        return warehouseOutRecordService.checkWarehouseOutRecord(warehouseOutRecord);
    }

    @GetMapping("/deptList")
    @ApiOperation("公司部门列表")
    public Result<List<Dept>> deptList() {
        List<Dept> list = deptService.list(Wrappers.lambdaQuery(Dept.class)
                .eq(Dept::getOrganId, WebUtils.getUser().getOrganId()));
        return Result.success(list);
    }

}

